
import 'package:flutter/material.dart';
import 'package:get/get.dart';
import 'package:study_demo/base/BaseController.dart';

import '../../../api/bean.dart';
import '../../../widgets/text_button.dart';
import '../../../widgets/text_view.dart';
import '../../../widgets/yz_text.dart';

class ResizeDialog extends StatefulWidget {
  final TextEditingController _widthController =
      TextEditingController(text: '100');

  final TextEditingController _heightController =
      TextEditingController(text: '100');

  bool isMaintainAspect;

  Function? onConfirm;

  ResizeDialog({this.onConfirm, this.isMaintainAspect = true, Key? key})
      : super(key: key);

  @override
  State<StatefulWidget> createState() {
    return _ResizeDialogState();
  }
}

class _ResizeDialogState extends State<ResizeDialog> {
  @override
  Widget build(BuildContext context) {
    return SimpleDialog(
        contentPadding: EdgeInsets.all(10),
        title: TextView('修改大小'),
        children: [
          SizedBox(
            height: 20,
          ),
          Row(
            children: [
              Text('宽*高'),
              MyTextField(
                controller: widget._widthController,
                width: 100,
                height: 35,
                margin: EdgeInsets.only(left: 10),
                keyboardType: TextInputType.number,
                hintText: '',
              ),
              Container(
                  margin: EdgeInsets.symmetric(horizontal: 10),
                  child: Text('*')),
              MyTextField(
                controller: widget._heightController,
                width: 100,
                height: 35,
                keyboardType: TextInputType.number,
                hintText: '',
              ),
            ],
          ),
          SizedBox(
            height: 20,
          ),
          Row(
            children: [
              Text(
                '保持纵横比',
                style: TextStyle(color: Colors.black),
              ),
              SizedBox(
                width: 10,
              ),
              Checkbox(
                  value: widget.isMaintainAspect,
                  onChanged: (value) {
                    setState(() {
                      widget.isMaintainAspect = !widget.isMaintainAspect;
                    });
                  })
            ],
          ),
          SizedBox(
            height: 10,
          ),
          Container(
              margin: EdgeInsets.symmetric(vertical: 10),
              child: Row(
                mainAxisAlignment: MainAxisAlignment.spaceAround,
                children: [
                  _textButton('确定', onPressed: () {
                    if (widget._widthController.text.isEmpty) {
                      toast('宽度不能为空');
                      return;
                    }
                    if (widget._heightController.text.isEmpty) {
                      toast('高度不能为空');
                    }
                    ImageSize imageSize = ImageSize();
                    imageSize.width = int.parse(widget._widthController.text);
                    imageSize.height = int.parse(widget._heightController.text);
                    imageSize.isMaintainAspect = widget.isMaintainAspect;
                    // widget.onConfirm?.call(imageInfo);
                    Get.back(result: imageSize);
                  }),
                  _textButton('取消', backgroundColor: Colors.grey,
                      onPressed: () {
                    // widget.onConfirm?.call();
                    Get.back();
                  })
                ],
              ))
        ]);
  }

  _textButton(String text, {Color? backgroundColor, Function? onPressed}) {
    return MyTextButton(
        text: text,
        width: 100,
        confirm: () {
          onPressed?.call();
        },
        backgroundColor: backgroundColor ?? Colors.deepPurpleAccent,
        textStyle: TextStyle(color: Colors.white, fontSize: 13));
  }
}

